import axios from 'axios';
const instance = axios.create({
  baseURL: 'http://127.0.0.1:8888/api/private/v1/', 
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' //设置服务器响应头
  },
  transformRequest: [function (data) {
    let newData = '';
    for (let k in data) {
      if (data.hasOwnProperty(k) === true) {
        newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
      }
    }
    return newData;
  }]
});

instance.interceptors.request.use(
  function (config) {
    config.headers.Authorization=window.sessionStorage.getItem("token");
    return config;
  }
)

instance.interceptors.response.use(
  function (result) {
    return result.data;
  },
  function (err) {
    return err;
  }
)
function apiAxios(method, url, params) {
  return instance({
    method: method,
    url: url,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null,
  })
}
export default {
  get: function (url, params) {
    return apiAxios('GET', url, params)
  },
  post: function (url, params) {
    return apiAxios('POST', url, params)
  },
  put: function (url, params) {
    return apiAxios('PUT', url, params)
  },
  delete: function (url, params) {
    return apiAxios('DELETE', url, params)
  },
  axiosall: function (axiosArray){
      return axios.all(axiosArray);
  }
}